﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WeatherFrame
{
	public partial class WeatherIcon : UserControl, IDataContextChangedHandler<WeatherIcon>
	{
		public WeatherIcon()
		{
			// Required to initialize variables
			InitializeComponent();
            SetFromString(null);
            DataContextChangedHelper<WeatherIcon>.Bind(this);
            SunPath.Data = Sun();
            CloudSunPath2.Data = RayPartial();
		}


        PathFigure Ray(double length) {
            return new PathFigure {
                StartPoint = new Point(0,-50), IsClosed = true,
                Segments = { 
                    new LineSegment { Point = new Point(-5,-50+length) },
                    new LineSegment { Point = new Point(5,-50+length) },
                }                
            };
        }

        Point pt1 = new Point(-5, -34);
        Point pt2 = new Point(5, -34);

        PathFigure Ray16()
        {
            return new PathFigure
            {
                StartPoint = new Point(0, -50),
                IsClosed = true,
                Segments = { 
                    new LineSegment { Point = pt1 },
                    new LineSegment { Point = pt2 }
                }
            };
        }


        static GeometryGroup sun;
        GeometryGroup Sun()
        {
            //if (sun == null)
            //{
                sun = new GeometryGroup();
                sun.Children.Add(new EllipseGeometry { RadiusX = 30, RadiusY = 30 });
                for (int i = 0; i < 12; i++)
                {
                    sun.Children.Add(
                        new PathGeometry
                        {
                            Figures = { Ray16() },
                            Transform = new RotateTransform { Angle = 15 + 30 * i }
                        });
                }
            //}
            return sun;
        }

        static GeometryGroup sunPartial;
        GeometryGroup RayPartial()
        {
            //if (sunPartial == null)
            //{
                sunPartial = new GeometryGroup();
                for (int i = 0; i < 12; i++)
                {
                    if ((i > 1) && (i < 7)) continue;
                    sunPartial.Children.Add(
                        new PathGeometry
                        {
                            Figures = { Ray(13) },
                            Transform = new RotateTransform { Angle = 15 + 30 * i }
                        });
                }
            //}
            return sunPartial;
        }

        #region IDataContextChangedHandler<WeatherIcon> Members
        static Brush transBrush = new SolidColorBrush(Color.FromArgb(0,0,0,0));
        static Brush greyBrush = new SolidColorBrush(Color.FromArgb(255,40,40,40));
        public void DataContextChanged(WeatherIcon sender, DependencyPropertyChangedEventArgs e)
        {
            SetFromString(DataContext as string);
        }

        static GridLength zeroLength = new GridLength(0);
        private void SetFromString(string s)
        {
            if (s == null) s = "";
            s = s.ToLowerInvariant();
            bool isSunOnly = s.Equals("sun");
            bool isSunWithCloud = s.Contains("sun") && !isSunOnly;
            bool isMoonOnly = s.Equals("moon");
            bool isMoonWithCloud = s.Contains("moon") && !isMoonOnly;
            bool isCloud = s.Contains("cloud");
            bool isThunder = s.Contains("thundercloud");
            bool isGreyCloud = s.Contains("greycloud") || isThunder;
            bool isRain = s.Contains("rain"); bool isSleet = s.Contains("sleet");
            bool isSnow = s.Contains("snow"); bool isHail = s.Contains("hail");
            bool isMist = s.Contains("mist"); bool isFog = s.Contains("fog");
            bool isTwoPrecip = (isGreyCloud || isSleet) && !isThunder; // are two things falling from the cloud
            FogPath.Visibility = isFog ? Visibility.Visible : Visibility.Collapsed;
            MistPath.Visibility = isMist ? Visibility.Visible : Visibility.Collapsed;
            CloudSunPath.Visibility = isSunWithCloud ? Visibility.Visible : Visibility.Collapsed;
            CloudSunPath2.Visibility = isSunWithCloud ? Visibility.Visible : Visibility.Collapsed;
            CloudMoonPath.Visibility = isMoonWithCloud ? Visibility.Visible : Visibility.Collapsed;
            CloudSunCol.Width = isSunWithCloud || isMoonWithCloud ? new GridLength(1, GridUnitType.Star) : zeroLength;
            CloudSunRow.Height = isSunWithCloud || isMoonWithCloud ? new GridLength(1, GridUnitType.Star) : zeroLength;
            SunPath.Visibility = isSunOnly ? Visibility.Visible : Visibility.Collapsed;
            MoonPath.Visibility = isMoonOnly ? Visibility.Visible : Visibility.Collapsed;
            CloudPath.Visibility = isCloud ? Visibility.Visible : Visibility.Collapsed;
            CloudPath.Fill = isGreyCloud ? greyBrush : transBrush;
            Raindrop.Visibility = isRain || isSleet ? Visibility.Visible : Visibility.Collapsed;
            Raindrop2.Visibility = (isRain & isGreyCloud) ? Visibility.Visible : Visibility.Collapsed;
            Hail.Visibility = isHail ? Visibility.Visible : Visibility.Collapsed;
            Hail2.Visibility = (isHail & isGreyCloud) ? Visibility.Visible : Visibility.Collapsed;
            Snow.Visibility = isSnow ? Visibility.Visible : Visibility.Collapsed;
            Snow2.Visibility = ((isSnow & isGreyCloud) || isSleet) ? Visibility.Visible : Visibility.Collapsed;
            Lightning.Visibility = isThunder ? Visibility.Visible : Visibility.Collapsed;
            SecondRaindropColumn.Width = isTwoPrecip ? new GridLength(2, GridUnitType.Star) : zeroLength;
           // RainRow.Height = isRain ? new GridLength(6, GridUnitType.Star) : zeroLength;
        }

        #endregion
    }
}